//
//  STDSRenderExporter.m
//  STDRenderSDK
//
//  Created by 陈学明 on 2023/3/16.
//

#import "STDSRenderExporter.h"
#import <OpenGLES/ES2/gl.h>
#import "STDSRenderThread.h"

@interface STDSRenderExporter ()
@property (nonatomic, strong) EAGLContext *context;
@property (nonatomic, strong, readwrite) STDSRenderer *renderer;
@property (nonatomic, strong) STDSRenderThread *renderThread;
@property (nonatomic, assign) CGSize composeSize;
@end

@implementation STDSRenderExporter

- (instancetype)initWithCompose:(CGSize)composeSize {
    self = [super init];
    if (self) {
        _composeSize = composeSize;
        _renderThread = [[STDSRenderThread alloc] init];
        [self createGL];
    }
    return self;
}

- (void)createGL {
    __weak STDSRenderExporter *weakSelf = self;
    [_renderThread addOperationWithBlock:^{
        weakSelf.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
        [EAGLContext setCurrentContext:weakSelf.context];
        weakSelf.renderer = [[STDSRenderer alloc] init];
        [weakSelf.renderer setComposeSizeSRC:weakSelf.composeSize];
        [weakSelf.delegate onReadyForExport:weakSelf];
    }];
}

#pragma mark - 渲染引擎相关
- (void)renderPrograss:(CGFloat)prograss {
    __weak STDSRenderExporter *weakSelf = self;
    [_renderThread addOperationWithBlock:^{
        int width = 0;
        int height = 0;
        unsigned char *data = NULL;
        [weakSelf.renderer readPixelOfPrograss:0.0 width:&width height:&height data:&data];
        if (data == NULL || width <= 0 || height <= 0) {
            return;
        }
        NSUInteger len = width * height * 4;
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, len, NULL);
        int bitsPerComponent = 8;
        int bitsPerPixel = 32;
        int bytesPerRow = 4 * width;
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
        CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
        CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
        
        UIGraphicsBeginImageContext(CGSizeMake(width, height));
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        CGImageRelease(imageRef);
        CGDataProviderRelease(provider);
        CGColorSpaceRelease(colorSpaceRef);
        free(data);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([weakSelf.delegate respondsToSelector:@selector(renderExport:renderFinishedWithImage:)]) {
                [weakSelf.delegate renderExport:weakSelf renderFinishedWithImage:image];
            }
        });
    }];
}

- (STDSGLImageParam *)addImageLayerWithImagePath:(NSString *)imagePath {
    return [self.renderer addImageLayerWithImagePath: imagePath];
}

- (void)addEffectType:(STDSEffectType)effectType {
    [self.renderer addEffectType:effectType];
}

- (void)destroy {
    __weak STDSRenderExporter *weakSelf = self;
    [self.renderThread addOperationWithBlock:^{
        [weakSelf.renderer destroyRenderEngine];
    }];
    [self.renderThread close];
}

- (void)dealloc {
//    [self destroy];
    NSLog(@"STDSRenderExporter--dealloc");
}
@end
